package com.xinsoft.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysFlowParam;
import com.xinsoft.entity.po.SysApproveFlow;
import com.xinsoft.service.SysApproveFlowService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName SysFlowController
 * @Description 流程管理
 * @Author lijie
 * @Date 2023/5/22 13:50 星期一
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/flow")
@Api(value = "SysFlowController", tags = {"流程管理控制层"})
public class SysFlowController {

    @Autowired
    private SysApproveFlowService  flowService;


    /**
     * @Author Li9527
     * @Description 流程管理-新增
     * @Date 2023/5/17 9:57
     * @params   dept
     * @return com.xinsoft.common.Response<?>
     **/
    @SysLog(title = "流程管理-新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增")
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysApproveFlow flow) {
        // 参数校验
        ValidatorUtils.validateEntity(flow);
        // 默认启用
        flow.setFlowStatus(Const.Y);
        SysApproveFlow sysApproveFlow = flowService.getOne(new QueryWrapper<SysApproveFlow>()
                        .eq("flow_name",flow.getFlowName())
                        .eq("tab_name",flow.getTabName())
                );
        if(sysApproveFlow != null){
            return Response.fail("该项流程及表单已存在,请不要重复添加");
        }
        return Response.succeed(flowService.save(flow));
    }

    /**
     * @Author Li9527
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params   dept
     * @return com.xinsoft.common.Response<?>
     **/
    @ApiOperation("更新")
    @SysLog(title = "流程管理-编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody SysApproveFlow flow) {
        // 参数校验
        ValidatorUtils.validateEntity(flow);
        SysApproveFlow sysApproveFlow = flowService.getOne(new QueryWrapper<SysApproveFlow>()
                .eq("flow_name",flow.getFlowName())
                .eq("tab_name",flow.getTabName())
        );
        if(sysApproveFlow != null && !flow.getId().equals(sysApproveFlow.getId())){
            return Response.fail("该项流程及表单已存在,请不要重复添加");
        }
        return Response.succeed(flowService.updateById(flow));
    }

    /**
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params   id
     * @return com.xinsoft.common.Response<?>
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(flowService.getById(id));
    }

    /**
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params   ids
     * @return com.xinsoft.common.Response<?>
     **/
    @ApiOperation("删除")
    @SysLog(title = "流程管理-删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        flowService.removeByIds(ids);
        return Response.succeed();
    }


    /**
     * @Author Li9527
     * @Description 流程管理-启用禁用
     * @Date 2023年5月23日10:04:43
     * @params   ids
     * @return com.xinsoft.common.Response<?>
     **/
    @ApiOperation("启用/禁用")
    @SysLog(title = "流程管理-启用禁用", businessType = BusinessType.OTHER)
    @PostMapping("/ctl")
    public Response<?> ctl(@RequestBody SysApproveFlow flow) {
        if(flow.getId() == null || flow.getFlowStatus() == null){
            return Response.fail("参数传递异常，id或状态不能为空");
        }
        SysApproveFlow old = flowService.getById(flow.getId());
        if(old.getFlowStatus().equals(flow.getFlowStatus())){
            return Response.fail("重复启用或禁用");
        }
        flowService.updateById(flow);
        return Response.succeed();
    }

    /**
     * @Author Li9527
     * @Description 流程管理分页
     * @Date 2023/5/18 10:51
     * @params   param
     * @return com.xinsoft.common.Response<?>
     **/
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody SysFlowParam param) {
        return Response.succeed(flowService.findPageListByParam(param));
    }






}
